package com.xyz.init.infos;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.xyz.business.bean.User;
import com.xyz.business.service.UserService;
import com.xyz.business.service.impl.UserHelper;
import com.xyz.constant.XYZConstant;

/**
 * 系统启动的时候加载用户信息
 * 
 * @author xyzjh
 * 
 */
public class UserInfosInit {
	private static Map<String, User> userMap = new HashMap<String, User>(64);
	private static List<User> users = new ArrayList<User>(16);

	/**
	 * 暴露给外部的初始化方法
	 * 
	 * @param userService
	 */
	public static synchronized void init(UserService userService) {
		userMap.clear();
		List<User> totalUserList = UserHelper.queryTotalUserList(userService);
		users = totalUserList;
		for (User user : totalUserList) {
			userMap.put(user.getUid(), user);
		}
	}

	/**
	 * 根据用户id拿到用户信息
	 * 
	 * @param uid
	 * @return
	 */
	public static User getUser(String uid) {
		return userMap.get(uid);
	}

	/**
	 * 根据用户id查找用户姓名
	 * 
	 * @param uid
	 * @return
	 */
	public static String getName(String uid) {
		if (StringUtils.isBlank(uid) || null == getUser(uid)) {
			return XYZConstant.EMPTY;
		}
		return getUser(uid).getName();
	}

	/**
	 * 获取用户列表
	 * @return
	 */
	public static List<User> getUserList() {
		return users;
	}
}
